package com.niit.activity.mapper;

import com.niit.activity.entity.Comment;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface CommentMapper {
    @Insert({"<script>",
      "INSERT INTO tb_comment (user_id, activity_id, parent_id, content, create_date, update_date, is_deleted, version)",
      "VALUES (#{userId}, #{activityId}, #{parentId}, #{content}, NOW(), NOW(), #{isDeleted}, #{version})",
      "</script>"})
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertComment(Comment comment);

    @Update({"<script>",
      "UPDATE tb_activity SET comment_num = comment_num + 1 WHERE id = #{activityId}",
      "</script>"})
    void incrementActivityCommentCount(@Param("activityId") Long activityId);


    @Select({"<script>",
      "SELECT c.* ",
      "FROM tb_comment c ",
      "JOIN tb_activity a ON c.activity_id = a.id ",
      "WHERE a.id = #{activityId} AND c.is_deleted = 0 AND c.parent_id = 0",
      "</script>"})
    List<Comment> selectCommentsByActivityId(@Param("activityId") Long activityId);

    @Select({"<script>",
      "SELECT c.* ",
      "FROM tb_comment c ",
      "WHERE c.parent_id = #{parentId} AND c.is_deleted = 0",
      "</script>"})
    List<Comment> selectCommentsByParentId(@Param("parentId") Long parentId);
}